package com.eastone.user.serverapi.listener;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

/**
 * @author liuxiangfei
 * @date 2016年7月20日 下午4:15:41
 * @version 1.0.0
 */
public class SessionContext {
	public static final String USERID = "USERID";
	private static SessionContext instance;
	private HashMap<String, HttpSession> sessionMap;

	private SessionContext() {
		sessionMap = new HashMap<String, HttpSession>();
	}

	public static SessionContext getInstance() {
		if (instance == null) {
			instance = new SessionContext();
		}
		return instance;
	}

	public synchronized void AddSession(HttpSession session) {
		if (session != null) {
			sessionMap.put(session.getId(), session);
		}
	}

	public synchronized void DelSession(HttpSession session) {
		if (session != null) {
			sessionMap.remove(session.getId());
			if (session.getAttribute(USERID) != null) {
				sessionMap.remove(session.getAttribute(USERID).toString());
			}
		}
	}

	public synchronized HttpSession getSession(String sessionId) {
		if (sessionId == null)
			return null;
		return (HttpSession) sessionMap.get(sessionId);
	}

	public HashMap<String, HttpSession> getSessionMap() {
		return sessionMap;
	}

	public void setMymap(HashMap<String, HttpSession> sessionMap) {
		this.sessionMap = sessionMap;
	}
}
